//
//  LRTools.m
//  Anyair
//
//  Created by karlcool on 14-5-25.
//  Copyright (c) 2014年 Karlcool. All rights reserved.
//

#import "LRTools.h"
#import "Reachability.h"

@implementation LRTools

+(BOOL)checkNetWork {
    Reachability *r = [Reachability reachabilityWithHostName:@"www.baidu.com"];
    if ([r currentReachabilityStatus]==NotReachable) {
        return NO;
    } else {
        return YES;
    }

}

+(NSString*)getNowTime {
    
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
    [dateFormatter setDateFormat:@"yyyyMMddHHmm"];
    NSString *nowDate = [dateFormatter stringFromDate:[NSDate date]];
    return nowDate;
}

+(NSString*)turnTimeByTime:(NSString*)time {
    
    NSMutableString *tempTime = [time mutableCopy];
    [tempTime insertString:@"-" atIndex:4];
    [tempTime insertString:@"-" atIndex:7];
    [tempTime insertString:@"  " atIndex:10];
    [tempTime insertString:@":" atIndex:14];
    return [tempTime copy];
}

+(NSString*)turntemperature:(NSString*)temper {
    
    NSString *resultStr = [NSString stringWithFormat:@"%i",(int)(temper.floatValue - 32)*5/9];
    return resultStr;
}

+(UIColor*)setColorForValue:(NSString*)value AndType:(int)dataType {

    if (dataType==0) {
        if (value.intValue<=20) {
            return [LRTools colorWithHexString:@"#05a760"];
        } else if (value.intValue>20&&value.intValue<=75) {
            return [LRTools colorWithHexString:@"#fda100"];
        } else if (value.intValue>75&&value.intValue<=120) {
            return [LRTools colorWithHexString:@"#e84c3d"];
        } else if (value.intValue>120&&value.intValue<=200) {
            return [LRTools colorWithHexString:@"#e60012"];
        } else {
            return [LRTools colorWithHexString:@"#e4007f"];
        }
    } else if (dataType==1) {
        if (value.intValue<=800) {
            return [LRTools colorWithHexString:@"#05a760"];
        } else if (value.intValue>800&&value.intValue<=1000) {
            return [LRTools colorWithHexString:@"#fda100"];
        } else if (value.intValue>1000&&value.intValue<=1200) {
            return [LRTools colorWithHexString:@"#e84c3d"];
        } else if (value.intValue>1200&&value.intValue<=1500) {
            return [LRTools colorWithHexString:@"#e60012"];
        } else {
            return [LRTools colorWithHexString:@"#e4007f"];
        }

    } else {
        if (value.floatValue<=0.03) {
            return [LRTools colorWithHexString:@"#05a760"];
        } else if (value.floatValue>0.03&&value.floatValue<=0.06) {
            return [LRTools colorWithHexString:@"#fda100"];
        } else if (value.floatValue>1000&&value.floatValue<=1200) {
            return [LRTools colorWithHexString:@"#e84c3d"];
        } else {
            return [LRTools colorWithHexString:@"#e60012"];
        }
    }
}

+(int)setValueForValue:(NSString*)value AndType:(int)dataType {
    if (dataType==0) {
        if (value.intValue<=20) {
            return 1;
        } else if (value.intValue>20&&value.intValue<=75) {
            return 27;
        } else if (value.intValue>75&&value.intValue<=120) {
            return 54;
        } else if (value.intValue>120&&value.intValue<=200) {
            return 81;
        } else {
            return 108;
        }
    } else if (dataType==1) {
        if (value.intValue<=800) {
            return 1;
        } else if (value.intValue>800&&value.intValue<=1000) {
            return 27;
        } else if (value.intValue>1000&&value.intValue<=1200) {
            return 54;
        } else if (value.intValue>1200&&value.intValue<=1500) {
            return 81;
        } else {
            return 108;
        }
        
    } else {
        if (value.floatValue<=0.03) {
            return 1;
        } else if (value.floatValue>0.03&&value.floatValue<=0.06) {
            return 27;
        } else if (value.floatValue>1000&&value.floatValue<=1200) {
            return 54;
        } else {
            return 81;
        }
    }
    
}

+(UIImage*)setAnnotationImageForValue:(NSString*)value AndType:(int)dataType {

    if (dataType==0) {
        if (value.intValue<=20) {
            return [UIImage imageNamed:@"Locater01"];
        } else if (value.intValue>20&&value.intValue<=75) {
            return [UIImage imageNamed:@"Locater02"];
        } else if (value.intValue>75&&value.intValue<=120) {
            return [UIImage imageNamed:@"Locater03"];
        } else if (value.intValue>120&&value.intValue<=200) {
            return [UIImage imageNamed:@"Locater04"];
        } else {
            return [UIImage imageNamed:@"Locater05"];
        }
    } else if (dataType==1) {
        if (value.intValue<=800) {
            return [UIImage imageNamed:@"Locater01"];
        } else if (value.intValue>800&&value.intValue<=1000) {
            return [UIImage imageNamed:@"Locater02"];
        } else if (value.intValue>1000&&value.intValue<=1200) {
            return [UIImage imageNamed:@"Locater03"];
        } else if (value.intValue>1200&&value.intValue<=1500) {
            return [UIImage imageNamed:@"Locater04"];
        } else {
            return [UIImage imageNamed:@"Locater05"];
        }
        
    } else {
        if (value.floatValue<=0.03) {
            return [UIImage imageNamed:@"Locater01"];
        } else if (value.floatValue>0.03&&value.floatValue<=0.06) {
            return [UIImage imageNamed:@"Locater02"];
        } else if (value.floatValue>1000&&value.floatValue<=1200) {
            return [UIImage imageNamed:@"Locater03"];
        } else {
            return [UIImage imageNamed:@"Locater04"];
        }
    }
}


+(UIColor *)colorWithHexString: (NSString *)color {
    NSString *cString = [[color stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] uppercaseString];
    if ([cString length] < 6) {
        return [UIColor clearColor];
    }
    if ([cString hasPrefix:@"0X"])
        cString = [cString substringFromIndex:2];
    if ([cString hasPrefix:@"#"])
        cString = [cString substringFromIndex:1];
    if ([cString length] != 6)
        return [UIColor clearColor];

    NSRange range;
    range.location = 0;
    range.length = 2;

    NSString *rString = [cString substringWithRange:range];
    range.location = 2;
    NSString *gString = [cString substringWithRange:range];
    range.location = 4;
    NSString *bString = [cString substringWithRange:range];
    
    unsigned int r, g, b;
    [[NSScanner scannerWithString:rString] scanHexInt:&r];
    [[NSScanner scannerWithString:gString] scanHexInt:&g];
    [[NSScanner scannerWithString:bString] scanHexInt:&b];
    
    return [UIColor colorWithRed:((float) r / 255.0f) green:((float) g / 255.0f) blue:((float) b / 255.0f) alpha:1.0f];
}

+ (BOOL)isPureFloat:(NSString*)string{
    
    NSScanner* scan = [NSScanner scannerWithString:string];
    float val;
    return[scan scanFloat:&val] && [scan isAtEnd];
    
}
@end
